const app = getApp()
const BASE_URL = 'https://www.beiyouge.net/api'       /* 线上 */
// const BASE_URL = 'http://10.168.1.144:9093/api'       /* 王玉龙 */
// baseUrl: "http://10.168.1.144:8383/mApi"      /* 王玉龙 */


function _requestResopnse(res, resolve, reject) {
  if (res.statusCode == 200) {
    const code = res.data.code;
    if (code == 0) {
      resolve(res.data);
      console.log(res)
    } else {
      reject(res);
    }
  } else {

  }
}

function httpPost(url, data = {}) {
  return new Promise((resolve, reject) => {
    tt.request({
      url: `${BASE_URL}${url}`,
      data: data,
      method: "POST",
      header: {
        "token": tt.getStorageSync('token'),
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        _requestResopnse(res, resolve, reject);
        //console.log(res)
      },
      fail: function (err) {
        reject("#1:网络出错");
      }
    });
  });
}

function httpGet(url, data = {}) {
  return new Promise((resolve, reject) => {
    tt.request({
      url: `${BASE_URL}${url}`,
      data: data,
      method: "GET",
      header: {
        "token": tt.getStorageSync('token'),
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        _requestResopnse(res, resolve, reject);
      },
      fail: function (err) {
        reject("#1:网络出错");
      }
    });
  });
}

export {
  httpPost,
  httpGet,
  BASE_URL
}